/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
*/
@isTest(seeAllData=TRUE)
private class RecordTypeHelperTest {

    static testMethod void GetRecordTypeIdTest() {
        
        List<RecordType> recordTypes = [select Id, Name, SobjectType, IsActive  from RecordType order by Name];
        
        test.startTest();
        
        for(RecordType rt : recordTypes)
        {
            ID recordTypeID = RecordTypeHelper.GetRecordTypeId(rt.SobjectType, rt.Name);
            if(rt.IsActive){
                if(recordTypeID != rt.Id){
                    System.debug('Loading record type for the ' + rt.SobjectType + ' object called ' + rt.Name + ' did not return the expected record type ID');
                    System.assertEquals(rt.Id, recordTypeID);
                }
            }
            else
            {
                if(recordTypeID != null){
                    System.debug('Loading record type for the ' + rt.SobjectType + ' object called ' + rt.Name + ' returned a record type ID. This is not expected since the record type is inactive');
                    System.assertEquals(null, recordTypeID);
                }
            }           
        }
        
        test.stopTest();         
    }

    static testMethod void GetRecordTypeIdByDevNameTest() {
        
        List<RecordType> recordTypes = [select Id, DeveloperName, SobjectType, IsActive  from RecordType order by Name];
        
        test.startTest();
        
        for(RecordType rt : recordTypes)
        {
            ID recordTypeID = RecordTypeHelper.GetRecordTypeIdByDevName(rt.SobjectType, rt.DeveloperName);
            if(rt.IsActive){
                if(recordTypeID != rt.Id){
                    System.debug('Loading record type for the ' + rt.SobjectType + ' object called ' + rt.DeveloperName + ' did not return the expected record type ID');
                    System.assertEquals(rt.Id, recordTypeID);
                }
            }
            else
            {
                if(recordTypeID != null){
                    System.debug('Loading record type for the ' + rt.SobjectType + ' object called ' + rt.DeveloperName + ' returned a record type ID. This is not expected since the record type is inactive');
                    System.assertEquals(null, recordTypeID);
                }
            }           
        }
        
        test.stopTest();         
    }
    
    static testMethod void PrepareRecordTypeIdTest() {
            
        Set<string> objectTypeNames = new Set<string>();
        List<RecordType> recordTypes = [select Id, Name, SobjectType, IsActive  from RecordType order by Name];
        
        for(RecordType rt : recordTypes){
            if(!objectTypeNames.contains(rt.SobjectType))
                objectTypeNames.add(rt.SobjectType);
        }
        
        test.startTest();
     
        
        // Prepare all objects for loading      
        for(string objectTypeName : objectTypeNames)
            RecordTypeHelper.PrepareToLoad(objectTypeName);

        // Just for fun we'll request one that's already been requeseted
        RecordTypeHelper.PrepareToLoad('aCCount');
        RecordTypeHelper.PrepareToLoad('Account');
        RecordTypeHelper.PrepareToLoad('Account');

        // Verify that all record type IDs were loaded
        for(RecordType rt : recordTypes)
        {
            ID recordTypeID = RecordTypeHelper.GetRecordTypeId(rt.SobjectType, rt.Name);
            if(rt.IsActive){
                if(recordTypeID != rt.Id){
                    System.debug('Loading record type for the ' + rt.SobjectType + ' object called ' + rt.Name + ' did not return the expected record type ID');
                    System.assertEquals(rt.Id, recordTypeID);
                }
            }
            else
            {
                if(recordTypeID != null){
                    System.debug('Loading record type for the ' + rt.SobjectType + ' object called ' + rt.Name + ' returned a record type ID. This is not expected since the record type is inactive');
                    System.assertEquals(null, recordTypeID);
                }
            }           
        }
       
        // Verify that only one query was performed
        System.assert(2 >= Limits.getQueries(), 'This code should not perform more than one query. On some sandboxes it seems to perform two (possibly because it breaks up the query into two because of a large number of results?');
        test.stopTest();         
    }
 
    static testMethod void PrepareRecordTypeIdTest2() {
    
        RecordTypeHelper.PrepareToLoad('Account');
        ID rt1 = RecordTypeHelper.GetRecordTypeId('Account', 'Standard Account Record Type');
        System.assertNotEquals(null, rt1);

        // Verify the SOQL limit
        System.assertEquals(Limits.getQueries(), 1);
        
        RecordTypeHelper.PrepareToLoad('Account');
        RecordTypeHelper.PrepareToLoad('Contact');

        ID rt2 = RecordTypeHelper.GetRecordTypeId('Contact', 'Standard Contact Record Type');
        
        // Verify the SOQL limit
        System.assertEquals(Limits.getQueries(), 2);
        
        ID rt3 = RecordTypeHelper.GetRecordTypeId('Account', 'Standard Account Record Type');
        
        // Verify the SOQL limit
        System.assertEquals(Limits.getQueries(), 2);
        
        System.assertNotEquals(null, rt2);
        System.assertNotEquals(null, rt3);
        System.assertEquals(rt1, rt3);
    }
            
    
}